package org.easy4j.framework.core.orm;

import java.util.BitSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *     最大支持64个字段
 * </p>
 *
 * @author bjliuyong
 * @version 1.0
 * @created date 15-10-23
 */
public class Mapping {

    /**
     * 总是把ID放在最前面
     */
    public static final int ID_INDEX = 0 ;

    private String[] fields  ;
    private String[] columns ;

    boolean hasId = false    ;

    BitSet  insertableBitSet ;
    int     insertableSize   ;

    BitSet  updatableBitSet  ;
    int     updatableSize    ;


    public Mapping(int size){
        fields = new String[size];
        columns = new String[size];
    }

    public String[] getColumns(){
        return this.columns ;
    }

    public String[] getFields(){
        return this.fields;
    }

    /**
     * 去除NULL空闲位置
     * @param size
     * @param idField
     */
    public void reset(int size ,String idField ,List<String> insertAbleList
            ,List<String> updatableList){

        String[] oldFields = fields ;
        String[] oldColumns = columns ;
        fields  = new String[size];
        columns = new String[size];
        int j = 0 ,IdIndex = 0 ;
        insertableBitSet = new BitSet(insertAbleList.size()) ;
        updatableBitSet  = new BitSet(updatableList.size())  ;

        for (int i = 0 ; i < oldFields.length ; i++ ) {

            if(oldFields[i] == null ) continue;

            fields[j]  = oldFields[i] ;
            columns[j] = oldColumns[i] ;

            if(insertAbleList.contains(fields[j])) {
                insertableBitSet.set(j);
                insertableSize++ ;
            }

            if(updatableList.contains(fields[j])){
                updatableBitSet.set(j);
                updatableSize++  ;
            }

            if( fields[j].equals(idField)) {
                IdIndex = j ;
                hasId   = true ;
            }

            j++ ;
        }

        if(IdIndex != 0) {
            swap0(IdIndex);
        }

    }

    private void swap0(int j) {
        int i = 0 ;
        String temp = fields[i] ;
        fields[i]   = fields[j] ;
        fields[j]   = temp ;

        temp        = columns[i];
        columns[i]  = columns[j];
        columns[j]  = temp ;

        boolean bt  = insertableBitSet.get(i);
        insertableBitSet.set(i , insertableBitSet.get(j));
        insertableBitSet.set(j,  bt );

        bt          = updatableBitSet.get(i);
        updatableBitSet.set(i , updatableBitSet.get(j));
        updatableBitSet.set(j,  bt );


    }

    void put(int position ,String key ,String value){
         fields[position]    = key ;
        columns[position]    = value ;
    }

    public Map<String,String> getColumnFieldMapping(){

        Map<String,String> mapping = new HashMap<String,String>(fields.length);
        int i = 0 ;
        for(String column : columns) {

            mapping.put(column ,fields[i++]);

        }

        return mapping ;

    }

    public boolean hasId(){
        return hasId ;
    }

    public static void main(String args[]){

        BitSet bitSet = new BitSet() ;
        bitSet.set(0);
        bitSet.set(1);

        bitSet.set(3);

        bitSet.set(63);

        bitSet.set(127);
        bitSet.set(149);


        bitSet.set(18762583);

        System.out.println(bitSet.get(0));
        System.out.println(bitSet.get(1));
        System.out.println(bitSet.get(2));
        System.out.println(bitSet.get(3));
        System.out.println(bitSet.get(18762583));

        System.out.println(bitSet.size());

    }

}
